package jianzhiOffer;

import java.util.Comparator;
import java.util.PriorityQueue;

public class num40 {
    public int[] getLeastNumbers(int[] arr, int k) {
        if(k == 0) return new int[0];
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.add(arr[i]);
            if(k < priorityQueue.size()) {
                priorityQueue.poll();
            }
        }
        int[] nums = new int[k];
        for (int i = 0; i < k; i++) {
            nums[i] = priorityQueue.poll();
        }
        return nums;
    }
}
